import 'package:flutter/material.dart';
import 'package:opene/app_theme.dart';

class LoadingDialog extends StatelessWidget {

  final String title;


  LoadingDialog(this.title);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        padding: const EdgeInsets.all(20.0),
        decoration: BoxDecoration(
          // 黑色背景
          color: MyAppTheme.grey,
          // 圆角边框
          borderRadius: BorderRadius.circular(10.0),
        ),
        child: Column(
          // 控件里面内容主轴负轴剧中显示
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          // 主轴高度最小
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const CircularProgressIndicator(),
            Padding(
              padding: const EdgeInsets.only(top: 10.0),
              child: Text(
                title,
                style: const TextStyle(
                    color: Colors.white,
                    fontSize: 16, // 调整文本大小
                    decoration: TextDecoration.none
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class LoadingIndicator {
  static BuildContext _context;
  static bool _isShowing = false;

  static void show(BuildContext context,{String title = 'Loading...'}) {
    try {
      hide();
    } catch (e) {
      e.toString();
    }
    try {
      if (!_isShowing) {
        showDialog(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            _context = context;
            return LoadingDialog(title);
          },
        );
        _isShowing = true;
      }
    } catch (e) {
      e.toString();
    }

  }

  static void hide() {
    try {
      if (_isShowing && _context != null) {
        Navigator.of(_context).pop();
        _isShowing = false;
      }
    } catch (e) {
      e.toString();
    }

  }
}